from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
phone_up0 = Api()

class Phone_Up1(Resource):
    def get(self):
        select = Phone_Up.query.all()
        return jsonify({"select": list(map(con7, select))})
    def post(self, uid):
        select = Phone_Up(
            request.json["time"],
            request.json["Top_up_object"],
            request.json["Phone"],
            request.json["Agent"],
            request.json["Approver"],
            request.json["Money"],
            request.json["Top_up_way"],
            request.json["cds"],

        )
        db.session.add(select)
        db.session.commit()
        return jsonify({"select": con7(select)}), 201


phone_up0.add_resource(Phone_Up1, "/Phone_Up1/")


class Phone_Up2(Resource):
    def get(self, uid):
        select = Phone_Up.query.filter_by(id=uid).first()
        if select is None:
            abort(404)
        return jsonify({"select": con7(select)})

    def put(self, uid):
        select = Phone_Up.query.get(uid)
        select.time = request.json.get("time")
        select.Top_up_object = request.json.get("Top_up_object")
        select.Phone = request.json.get("Phone")
        select.Agent = request.json.get("Agent")
        select.Approver = request.json.get("Approver")
        select.Money = request.json.get("Money")
        select.Top_up_way = request.json.get("Top_up_way")
        select.cds = request.json.get("cds")
        db.session.add(select)
        db.session.commit()
        return jsonify({"select": con7(select)})


phone_up0.add_resource(Phone_Up2, "/Phone_Up2/<int:uid>/")

